<template>
  <div
    ref="home"
    v-loading:a.b.c="loading"
    class="home-container"
    @wheel="wheel"
  >
    <ul
      :style="{
        marginTop,
      }"
      class="carousel-container"
      @transitionend="transitionend"
    >
      <li v-for="item in data" :key="item.id">
        <carousel-item :carousel="item"></carousel-item>
      </li>
    </ul>

    <div v-show="index >= 1" class="icon icon-up" @click="switchTo(index - 1)">
      <Icon type="arrowUp" />
    </div>
    <div
      v-show="data.length >= 1 && !loading"
      class="icon icon-down"
      @click="switchTo(index + 1)"
    >
      <Icon type="arrowDown" />
    </div>
    <div v-show="data.length >= 1 && !loading" class="indicator">
      <li
        :class="{
          active: i === index,
        }"
        @click="switchTo(i)"
        v-for="(item, i) in data"
        :key="item.id"
      ></li>
    </div>

    <!-- <Loading v-show="isLoading"></Loading> -->
  </div>
</template>

<script>
// import fetchData from "@/mixins/fetchData.js";
import Loading from "@/components/Loading/index.vue";
import CarouselItem from "./CarouselItem.vue";
import { getNewBanner } from "@/api/index.js";
import Icon from "@/components/Icon/index.vue";
import { mapState, mapActions } from "vuex";
export default {
  //   mixins: [fetchData([])],
  name: "Home",
  components: { CarouselItem, Icon, Loading },
  data: () => ({
    index: 0, //当前显示的是第几张轮播图
    contailerHeight: 0, // 整个容器的高度
    switching: false, // 是否正在翻页中
  }),
  created() {
    this.fetchBanner();
  },
  mounted() {
    this.handleResize();
    window.addEventListener("resize", this.handleResize);
  },
  destoryed() {
    window.removeEventListener("resize", this.handleResize);
  },
  computed: {
    ...mapState("banner", ["loading", "data"]),
    marginTop() {
      console.log(this.index, this.contailerHeight);
      return -this.index * this.contailerHeight + "px";
    },
  },
  methods: {
    ...mapActions("banner", ["fetchBanner"]),
    // async fetchData() {
    //   return await getNewBanner();
    // },
    handleResize() {
      this.contailerHeight = this.$refs.home.clientHeight;
    },
    switchTo(i) {
      this.index = i;
    },
    wheel(e) {
      if (this.switching || (e.deltaY <= 5 && e.deltaY >= -5)) return;

      if (e.deltaY < -5 && this.index > 0) {
        this.switching = true; // 上一个
        this.index--;
      } else if (e.deltaY > 5 && this.index < this.data.length - 1) {
        this.switching = true; // 下一个
        this.index++;
      }
    },
    transitionend() {
      // 过渡结束
      this.switching = false;
    },
  },
};
</script>

<style lang="less" scoped>
@import "~@/assets/style/mixin.less";
@import "~@/assets/style/var.less";
.home-container {
  width: 100%;
  height: 100%;
  //   background: @dark;
  position: relative;
  //
  ul {
    margin: 0;
    list-style: none;
    padding: 0;
  }
  .carousel-container {
    .pos-fill();
    transition: 500ms;
    li {
      width: 100%;
      height: 100%;
    }
  }
  .icon {
    transform: translateX(-50%);
    .pos-center();
    color: @gray;
    @gap: 25px;
    font-size: 30px;
    &.icon-up {
      top: @gap;
      animation: jump-up 2s infinite;
    }
    &.icon-down {
      top: auto;
      bottom: @gap;
      animation: jump-down 2s infinite;
    }
    @jump: 5px;
    @keyframes jump-up {
      0% {
        transform: translate(-50%, @jump);
      }
      50% {
        transform: translate(-50%, -@jump);
      }
      100% {
        transform: translate(-50%, @jump);
      }
    }
    @keyframes jump-down {
      0% {
        transform: translate(-50%, -@jump);
      }
      50% {
        transform: translate(-50%, @jump);
      }
      100% {
        transform: translate(-50%, -@jump);
      }
    }
  }
  .indicator {
    .pos-center();
    transform: translateY(-50%);
    left: auto;
    right: 20px;
    li {
      width: 7px;
      height: 7px;
      border-radius: 50%;
      list-style: none;
      background: @words;
      cursor: pointer;
      border: 1px solid #fff;
      box-sizing: border-box;
      transition: 0.5s;
      margin-bottom: 10px;
      &.active {
        width: 10px;
        height: 10px;
      }
    }
  }
}
</style>
